#include"AlgFloyd.h" 

#include"Field.h"
#include"Map.h"

#include<stdio.h>

AlgFloyd::AlgFloyd(const Map *map){
	m_map = map;
	printf("%d\n", m_graph[1][1][1][1]);
}

AlgFloyd::~AlgFloyd(){
	
}

void AlgFloyd::run(){
	//Initializing m_graph...
	for(int i = 0; i < NUM_ROWS; i++){
		for(int j = 0; j < NUM_COLS; j++){
			for(int k = 0; k < NUM_ROWS; k++){
				for(int l = 0; l < NUM_COLS; l++){
					if(i==k&&j==l)
						m_graph[i][j][k][l] = 0;
					else
						m_graph[i][j][k][l] = INFINITY;
				}
			}
		}
	}
	//for each edge (u,v)
	//dist[u][v] ← w(u,v)  // the weight of the edge (u,v)
	for(int i = 0; i < NUM_ROWS; i++){
		for(int j = 0; j < NUM_COLS; j++){
			Field *from = m_map->field(i,j);
			const QList<Field*> &neighbours = from->neighbours();
			for(int k = 0; k < neighbours.size(); k++)
				m_graph[i][j][neighbours[k]->row()][neighbours[k]->col()] = neighbours[k]->movementCost();
		}
	}
	for(int k1=0; k1<NUM_ROWS; k1++){
	for(int k2=0; k2<NUM_COLS; k2++){
		for(int i1 = 0; i1 < NUM_ROWS; i1++){
		for(int i2 = 0; i2 < NUM_COLS; i2++){
			for(int j1 = 0; j1 < NUM_ROWS; j1++){
			for(int j2 = 0; j2 < NUM_COLS; j2++){
				if( m_graph[i1][i2][k1][k2] + m_graph[k1][k2][j1][j2] < m_graph[i1][i2][j1][j2] ){
					m_graph[i1][i2][j1][j2] = m_graph[i1][i2][k1][k2] + m_graph[k1][k2][j1][j2];
				}
			}
			}
		}
		}
	}
	}
}

int AlgFloyd::cost(int i, int j, int k, int l)const{
	return m_graph[i][j][k][l];
}
